package com.larissa.android.quizdemo_viewbinding;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.larissa.android.quizdemo_viewbinding.databinding.ActivityMainBinding;


public class MainActivity extends AppCompatActivity {
    private ActivityMainBinding mActivityMainBinding;

    private int count=0;
    private int mCurrentIdx=0;

    private Question[] mQuestions=new Question[]{
            new Question(R.string.test_bj,true),
            new Question(R.string.test_dc,false),
            new Question(R.string.test_london,true),
            new Question(R.string.test_tokyo,true)
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivityMainBinding=ActivityMainBinding.inflate(getLayoutInflater());
        View view=mActivityMainBinding.getRoot();
        setContentView(view);

        mActivityMainBinding.btnTrue.setOnClickListener(view1 -> {
            if(mQuestions[mCurrentIdx].isQuestionAnswer())
                count++;
            updateQuestion();
        });

        mActivityMainBinding.btnFalse.setOnClickListener(view1->{
            if(!mQuestions[mCurrentIdx].isQuestionAnswer())
                count++;
            updateQuestion();
        });

        showQuestion();
    }

    private void updateQuestion(){
        mCurrentIdx++;
        if(mCurrentIdx==mQuestions.length){
            mActivityMainBinding.btnTrue.setEnabled(false);
            mActivityMainBinding.btnFalse.setEnabled(false);
            String toastText=String.format("%s questions are right.",count);
            Toast.makeText(MainActivity.this,toastText,Toast.LENGTH_LONG).show();
        }else{
            showQuestion();
        }
    }
    private void showQuestion(){
        int question=mQuestions[mCurrentIdx].getQuestionId();
        mActivityMainBinding.txQuestion.setText(question);
    }
}